﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PersonalExpenseTracker.PersistenceLayer;

namespace PersonalExpenseTracker
{
    public partial class CategoryUserControl : UserControl
    {
        public CategoryUserControl()
        {
            InitializeComponent();
        }

        private void CategoryUserControl_Load(object sender, EventArgs e)
        {
            List<FlyWeightItem> categories = 
                PersistenceLayerFacade.GetRootCategories();

            foreach (FlyWeightItem category in categories)
            {
                TreeNode node = new TreeNode();
                node.Text = category.Name;
                node.Tag = category.Id;
                node.ContextMenuStrip = mnuTreeView;
                trvCategory.Nodes.Add(node);
                LoadChildNodes(node);
            }

            if (trvCategory.Nodes.Count > 0)
            {
                trvCategory.SelectedNode = trvCategory.Nodes[0];
            }
            trvCategory.ExpandAll();
        }

        private void LoadChildNodes(TreeNode parentNode)
        {
            List<FlyWeightItem> categories =
                PersistenceLayerFacade.GetChildCategories((int)parentNode.Tag);

            foreach (FlyWeightItem category in categories)
            {
                TreeNode node = new TreeNode();
                node.Text = category.Name;
                node.Tag = category.Id;
                node.ContextMenuStrip = mnuTreeView;
                parentNode.Nodes.Add(node);
                LoadChildNodes(node);
            }
        }

        private void mnuNew_Click(object sender, EventArgs e)
        {
            if (trvCategory.SelectedNode == null)
            {
                return;
            }

            using (ManageCategory form = new ManageCategory((int)trvCategory.SelectedNode.Tag))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    TreeNode node = new TreeNode();
                    node.Text = form.CategoryName;
                    node.Tag = form.Id;
                    node.ContextMenuStrip = mnuTreeView;
                    trvCategory.SelectedNode.Nodes.Add(node);
                    trvCategory.SelectedNode.Expand();
                }
            }
        }

        private void mnuEdit_Click(object sender, EventArgs e)
        {
            if (trvCategory.SelectedNode == null)
            {
                return;
            }

            int parentId = 0;
            if (trvCategory.SelectedNode.Parent != null)
            {
                parentId = (int)trvCategory.SelectedNode.Parent.Tag;
            }

            using (ManageCategory form = new ManageCategory(
                (int)trvCategory.SelectedNode.Tag,
                trvCategory.SelectedNode.Text, parentId))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    trvCategory.SelectedNode.Text = form.CategoryName;
                }
            }
        }

        private void mnuRemove_Click(object sender, EventArgs e)
        {
            if (trvCategory.SelectedNode == null)
            {
                return;
            }

            if (MessageBox.Show("Are you sure you want to delete the selected category?", "Confirm Delete", MessageBoxButtons.YesNo)
                == DialogResult.Yes)
            {
            }
        }

        private void trvCategory_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void trvCategory_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeNode node = trvCategory.GetNodeAt(e.X, e.Y);

                if (node != null)
                {
                    trvCategory.SelectedNode = node;
                }
            }
        }
    }
}
